package com.anbinh.doctor.fragment.account;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.anbinh.doctor.DetailtActivity;
import com.anbinh.doctor.R;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentAddBanggia extends SherlockFragment {
	private static FragmentAddBanggia fragment = null;
	private View mView;
	private CircleProgressBar loading;
	private EditText edService;
	private EditText edPrice;

	public static FragmentAddBanggia newInstance() {
		if (fragment == null) {
			fragment = new FragmentAddBanggia();
		}
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu,
			com.actionbarsherlock.view.MenuInflater inflater) {
		menu.add(0, R.id.dia_diem_kham, Menu.NONE, "Hoàn thành")
				.setIcon(R.drawable.ic_done)
				.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		super.onCreateOptionsMenu(menu, inflater);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		pushBangGia();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onResume() {
		super.onResume();
		((DetailtActivity) getActivity()).setActionBarTitle(getResources()
				.getString(R.string.them_bang_gia));

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_them_bang_gia, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		edService = (EditText) mView.findViewById(R.id.service_content);
		edPrice = (EditText) mView.findViewById(R.id.service_price);
	}

	public void pushBangGia() {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		String token = userLogin.getData().getToken();
		String content_price = edService.getText().toString();

		if (content_price.isEmpty()) {
			Utils.showMessage(getActivity(),
					getResources()
							.getString(R.string.vui_long_nhap_ten_dich_vu));
			return;
		}

		String price = edPrice.getText().toString().trim();
		if (price.isEmpty()) {
			Utils.showMessage(getActivity(),
					getResources().getString(R.string.vui_long_nhap_gia));
			return;
		}
		loading.setVisibility(View.VISIBLE);

		ServerController.getInstance(getActivity()).requestAddPriceList(token,
				content_price, price, -1, new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						loading.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;

						Intent intent = new Intent();
						intent.putExtra(Utils.KEY, Utils.RESUME_PRICE);
						getActivity().setResult(Integer.MAX_VALUE, intent);
						getActivity().finish();

						Utils.showMessage(getActivity(), data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {
						loading.setVisibility(View.INVISIBLE);

					}

				});

	}
}
